package cn.fl.spl.controller;

import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysUsrDTO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.ISplRenewProtocolRecService;
import cn.fl.spl.vo.SplRenewProtocolRecVO;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.vo.SysUlFilesRecVO;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.condition.SplRenewProtocolRecQC;
import fl.spl.facade.spl.constant.SplInfoConstant;
import fl.spl.facade.spl.dto.SplInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 供应商续签协议记录 控制层
 */
@Controller
@RequestMapping("/spl/splReProtocol")
public class SplRenewProtocolRecController extends BaseController {

    /**
     * SplRenewProtocolRec 服务层BEAN
     */
    @Autowired
    private ISplRenewProtocolRecService splRenewProtocolRecService;
    /**
     * 供应商
     */
    @Autowired
    private ISplInfoApiService splInfoApiService;
    /**
     * 附件记录
     */
    @Autowired
    private ISysUlFilesRecService sysUlFilesRecService;
    /**
     * 用户
     */
    @Autowired
    private ISysUsrApiService sysUsrApiService;
    /**
     * 流程历史
     */
    @Autowired
    private IFlowQueryApiService flowQueryApiService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView view = new ModelAndView("spl/splRenewProtocolRec/splRenewProtocolRecList");

        return view;
    }

    /**
     * 到新增修改页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView view = new ModelAndView("spl/splRenewProtocolRec/splRenewProtocolRecAdd");

        return view;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");
        ModelAndView view = new ModelAndView("spl/splRenewProtocolRec/splRenewProtocolRecModify");
        view.addObject("id", id);
        return view;
    }

    /**
     * 到详情页面
     */
    @RequestMapping(value = "/toDetailPage.do", method = {RequestMethod.GET})
    public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");
        ModelAndView view = new ModelAndView("spl/splRenewProtocolRec/splRenewProtocolRecDetail");
        view.addObject("id", id);

        return view;
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {

        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String splId = request.getParameter("splId");// 供应商ID
        String isNew = request.getParameter("isNew");
        
        if (StringUtils.isEmpty(splId)) {
            BaseLogUtils.newWebLogger("供应商ID为空").setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0001);
			return null;
		}
        
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        SplRenewProtocolRecQC qc = new SplRenewProtocolRecQC();
        qc.setSplId(Long.valueOf(splId));
        qc.setIsDel(BaseConstant.IS_YESNO_NO);
        qc.setIsValid(BaseConstant.IS_YESNO_YES);
        if (StringUtils.isNotEmpty(isNew)) {
			qc.setIsNew(Integer.valueOf(isNew));
		}

        page.setCondition(qc);

        Pager pager = this.splRenewProtocolRecService.searchSplRePtlRecVOByPage(page);// 查询协议对象
        List<SplRenewProtocolRecVO> list = (List<SplRenewProtocolRecVO>) pager.getResultList();
        if (CollectionUtils.isEmpty(list)) {
            BaseLogUtils.newWebLogger("查询结果为空").setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return new ListPageVO<SplRenewProtocolRecVO>(new ArrayList<SplRenewProtocolRecVO>(), 0);
        }
        BaseLogUtils.newWebLogger("查询结果" + JSONObject.toJSONString(list)).setLogOperType(LogOperEnum.RESP)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        List<Long> usrIdList = new ArrayList<Long>();
        List<String> idList = new ArrayList<String>();
        for (SplRenewProtocolRecVO splRenewProtocolRecVO : list) {
			usrIdList.add(splRenewProtocolRecVO.getCrtUsrId());
			idList.add(splRenewProtocolRecVO.getId().toString());
		}
        
        // 查询对应用户
        SysUsrQueryCondition usrCondition = new SysUsrQueryCondition();
        usrCondition.setUsrIds(usrIdList);
        usrCondition.setSysCd(SplSystemConstant.FL_SYS_CD);
        List<SysUsrDTO> usrList = this.sysUsrApiService.searchSysUsrList(usrCondition);
        BaseLogUtils.newWebLogger("查询对应用户结果=>" + JSONObject.toJSONString(usrList)).setLogOperType(LogOperEnum.RESP)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        Map<Long, SysUsrDTO> usrMap = new HashMap<Long, SysUsrDTO>();
        for (SysUsrDTO sysUsrDTO : usrList) {
			usrMap.put(sysUsrDTO.getId(), sysUsrDTO);
		}
        
        // 查询对应附件记录
        SysUlFilesRecQueryCondition fileCondition = new SysUlFilesRecQueryCondition();
        fileCondition.setPkIdList(idList);
        fileCondition.setIsDel(0);
        fileCondition.setSysCd(SplSystemConstant.SPL_SYS_CD);
        List<SysUlFilesRecVO> fileList = this.sysUlFilesRecService.searchList(fileCondition);
        BaseLogUtils.newWebLogger("查询对应附件记录=>" + JSONObject.toJSONString(fileList)).setLogOperType(LogOperEnum.RESP)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        Map<String, SysUlFilesRecVO> fileMap = new HashMap<String, SysUlFilesRecVO>();
        for (SysUlFilesRecVO sysUlFilesRecVO : fileList) {
			fileMap.put(sysUlFilesRecVO.getPkId(), sysUlFilesRecVO);
		}
        
        for (SplRenewProtocolRecVO protVo : list) {
			if (usrMap.containsKey(protVo.getCrtUsrId())) {
				String refRlNm = usrMap.get(protVo.getCrtUsrId()).getRefRlNm();
				protVo.setCrtUsrNm(refRlNm);
			}
			if (fileMap.containsKey(protVo.getId().toString())) {
				protVo.setFileNm(fileMap.get(protVo.getId().toString()).getFileNm());
				protVo.setFilePath(fileMap.get(protVo.getId().toString()).getFilePath());
			}
		}
        
        ListPageVO<SplRenewProtocolRecVO> listPage = new ListPageVO<SplRenewProtocolRecVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {

        String splId = request.getParameter("splId");// 供应商ID
        String startDt = request.getParameter("startDt");// 协议开始时间
        String endDt = request.getParameter("endDt");// 协议终止时间
        String renewId = request.getParameter("renewId");// 历史最新协议ID
        String filePath = request.getParameter("filePath");// 附件路径
        String fileNm = request.getParameter("fileNm");// 附件名称
        String dataId = request.getParameter("dataId");// 上一条记录的ID
        
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
        
        if (StringUtils.isEmpty(splId)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("供应商ID为空");
			BaseLogUtils.newWebLogger("供应商ID为空").setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return result;
		}
        
        // 查询并修改前一条记录
        try {
        	if (StringUtils.isNotEmpty(dataId)) {
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_200, "驳回后修改记录,正常==>" + 
						BaseExceptionConstant.BASE_EXCEPTION_CODE_200_NAME);
			}
			SplRenewProtocolRecQC qc = new SplRenewProtocolRecQC();
			qc.setIsDel(BaseConstant.IS_YESNO_NO);
			qc.setIsValid(BaseConstant.IS_YESNO_YES);
			qc.setIsNew(2);
			qc.setSplId(Long.valueOf(splId));
			List<SplRenewProtocolRecVO> voList = this.splRenewProtocolRecService.searchSplRePtlRecVOList(qc);
			if (CollectionUtils.isNotEmpty(voList)) {
				SplRenewProtocolRecVO vo = voList.get(0);
				
				// 查询当前记录是否在审核中
		        SysFlowSHQueryCondition flowCondition = new SysFlowSHQueryCondition();
		        flowCondition.setDataId(vo.getId());
		        flowCondition.setSysCd(SplSystemConstant.FL_SYS_CD);
		        flowCondition.setWordsInTheTable(SplInfoConstant.SPL_FLOW_S_H_SPL);
		        flowCondition.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
		        List<SysFlowSHDTO> flowSHList = this.flowQueryApiService.searchFlowSHList(flowCondition);
		        if (CollectionUtils.isNotEmpty(flowSHList)) {
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					String info = "已发起协议续签流程";
					result.setInfo(info);
					return result;
				}
				
				vo.setIsNew(BaseConstant.IS_YESNO_NO);
				vo.setMdfTm(new Date());
				vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				boolean flag = this.splRenewProtocolRecService.modifySplRePtlRecVO(vo);
				if (flag) {
					BaseLogUtils.newWebLogger("修改上一条记录成功").setLogOperType(LogOperEnum.RESP)
                            .setSysCd(CurrentThreadContext.getCurrentSysCd())
                            .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                            .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                            .setWarning(false)
                            .info();
				} else {
                    BaseLogUtils.newWebLogger("修改上一条记录失败").setLogOperType(LogOperEnum.RESP)
                            .setSysCd(CurrentThreadContext.getCurrentSysCd())
                            .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                            .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                            .setWarning(false)
                            .warn(LogExceptionEnum.COMMON_CODE_0002);
				}
			}
        } catch (BaseException ex) {
            BaseLogUtils.newWebLogger("修改上一条记录异常,业务异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            BaseLogUtils.newWebLogger("修改上一条记录异常,系统异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .error(LogExceptionEnum.COMMON_CODE_0099);
        }
        
        SplInfoDTO splInfoDTO = this.splInfoApiService.selectSplInfoById(Long.valueOf(splId));
        BaseLogUtils.newWebLogger("供应商信息=>" + JSONObject.toJSONString(splInfoDTO)).setLogOperType(LogOperEnum.RESP)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        SplRenewProtocolRecVO entity = null;
        Long id = null;
        try {
        	if (StringUtils.isEmpty(dataId)) {// 上一条记录ID为空则执行添加操作
				// 添加合同续签记录
				entity = new SplRenewProtocolRecVO();
				// ADD PARAM
				entity.setSplId(Long.valueOf(splId));// 供应商ID
				entity.setSplNm(splInfoDTO.getSplNm());// 供应商名称
				if (StringUtils.isNotEmpty(startDt) && StringUtils.isNotEmpty(endDt)) {
					entity.setProtocolStartTm(DateUtils.formatStringToDate(startDt, DateUtils.YYYY_MM_DD));// 协议开始时间
					entity.setProtocolEndTm(DateUtils.formatStringToDate(endDt, DateUtils.YYYY_MM_DD));// 协议终止时间
				}
				entity.setRenewStsCd("0");// 续签状态(SPL0035，未续签："0"；续签中:"1"；已续签: "2"；续签不通过:"3")
				entity.setAudStsCd("0");// 协议审核状态
				entity.setIsValid(BaseConstant.IS_YESNO_YES);// 是否有效
				entity.setIsDel(BaseConstant.IS_YESNO_NO);// 是否删除
				entity.setIsNew(2);// 是否最新 2:添加续签协议记录,但没有发起续签流程
				if (StringUtils.isNotEmpty(renewId)) {
					entity.setRenewId(Long.valueOf(renewId));// 续签协议ID
				}
				entity.setCrtTm(new Date());// 创建时间-上传时间
				entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());// 创建人ID
				entity.setProtocolSource(SplSystemConstant.FL_WEB_CHL_CD);// 协议签署来源(渠道code)
				entity.setIsRemind(BaseConstant.IS_YESNO_NO + "");// 是否短信提醒
				BaseLogUtils.newWebLogger("插入的数据=>" + JSONObject.toJSONString(entity)).setLogOperType(LogOperEnum.RESP)
                        .setSysCd(CurrentThreadContext.getCurrentSysCd())
                        .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                        .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                        .setWarning(false)
                        .info();
				id = this.splRenewProtocolRecService.addSplRePtlRecVO(entity);// 添加合同续签记录
				result.setData(id);
				BaseLogUtils.newWebLogger("新增数据.[id=" + id + "] DATA:" + JSONObject.toJSONString(entity))
                        .setLogOperType(LogOperEnum.RESP)
                        .setSysCd(CurrentThreadContext.getCurrentSysCd())
                        .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                        .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                        .setWarning(false)
                        .info();
			} else {// 上一条记录ID不为空则执行修改操作
				entity = this.splRenewProtocolRecService.selectSplRePtlRecVOById(Long.valueOf(dataId));
				if (null != entity) {
					if (StringUtils.isNotEmpty(startDt) && StringUtils.isNotEmpty(endDt)) {
						entity.setProtocolStartTm(DateUtils.formatStringToDate(startDt, DateUtils.YYYY_MM_DD));// 协议开始时间
						entity.setProtocolEndTm(DateUtils.formatStringToDate(endDt, DateUtils.YYYY_MM_DD));// 协议终止时间
					}
					entity.setMdfTm(new Date());
					entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					boolean flag = this.splRenewProtocolRecService.modifySplRePtlRecVO(entity);
					if (flag) {
                        BaseLogUtils.newWebLogger("驳回后再提交-修改成功").setLogOperType(LogOperEnum.RESP)
                                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                                .setWarning(false)
                                .info();
					} else {
                        BaseLogUtils.newWebLogger("驳回后再提交-修改失败").setLogOperType(LogOperEnum.RESP)
                                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                                .setWarning(false)
                                .warn(LogExceptionEnum.COMMON_CODE_0002);
					}
				} else {
                    BaseLogUtils.newWebLogger("修改上一条记录失败").setLogOperType(LogOperEnum.RESP)
                            .setSysCd(CurrentThreadContext.getCurrentSysCd())
                            .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                            .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                            .setWarning(false)
                            .warn(LogExceptionEnum.COMMON_CODE_0002);
				}
			}
        	
			if (StringUtils.isEmpty(dataId)) {
				// 添加附件
				SysUlFilesRecVO fileVO = new SysUlFilesRecVO();
				fileVO.setSysCd(SplSystemConstant.SPL_SYS_CD);
				fileVO.setOrgCd(SplSystemConstant.DEFAULT_ORG_CD);
				fileVO.setCfgCd("SPL015");
				fileVO.setCfgDtlCd("10");
				if (StringUtils.isNotEmpty(id)) {
					fileVO.setPkId(String.valueOf(id));
				}
				fileVO.setTabNm("SPL_RENEW_PROTOCOL_REC");
				fileVO.setFilePath(filePath);
				fileVO.setFileNm(fileNm);
				fileVO.setAudStsCd("0");
				fileVO.setTransFlg(BaseConstant.IS_YESNO_NO);
				fileVO.setIsDel(BaseConstant.IS_YESNO_NO);
				fileVO.setCrtTm(new Date());
				fileVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				Long add = this.sysUlFilesRecService.add(fileVO);
				BaseLogUtils.newWebLogger("附件添加成功=>id = " + add).setLogOperType(LogOperEnum.RESP)
                        .setSysCd(CurrentThreadContext.getCurrentSysCd())
                        .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                        .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                        .setWarning(false)
                        .info();
			} else {
				SysUlFilesRecQueryCondition fileCondition = new SysUlFilesRecQueryCondition();
				fileCondition.setSysCd(SplSystemConstant.SPL_SYS_CD);
				fileCondition.setOrgCd(SplSystemConstant.DEFAULT_ORG_CD);
				fileCondition.setPkId(dataId);
				fileCondition.setIsDel(BaseConstant.IS_YESNO_NO);
				
				Map<String, Object> mdfMap = new HashMap<String, Object>();
				mdfMap.put("filePath", filePath);
				mdfMap.put("fileNm", fileNm);
				String rootPath = PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH);
				mdfMap.put("absLocPath", rootPath + filePath);
				
				DataResultDTO updateSysUlFilesRecByQC = this.sysUlFilesRecService.updateSysUlFilesRecByQC(fileCondition, mdfMap);
				BaseLogUtils.newWebLogger("修改附件=>" + JSONObject.toJSONString(updateSysUlFilesRecByQC))
                        .setLogOperType(LogOperEnum.RESP)
                        .setSysCd(CurrentThreadContext.getCurrentSysCd())
                        .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                        .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                        .setWarning(false)
                        .info();
			}
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.newWebLogger("新增对象失败,业务异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.newWebLogger("新增对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .error(LogExceptionEnum.COMMON_CODE_0099);
        }
        return result;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {

        String id = request.getParameter("id");
        //String name = request.getParameter("name");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("数据不存在!");
            return result;
        }
        SplRenewProtocolRecVO entity = null;
        try {
            entity = new SplRenewProtocolRecVO();
            entity.setId(Long.valueOf(id));
            //entity.setName(name);
            //TODO ADD PARAM
            boolean success = this.splRenewProtocolRecService.modifySplRePtlRecVO(entity);
            if (!success) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("修改失败!");
            }
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
        }

        return result;
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeById.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
            return result;
        }
        try {
            this.splRenewProtocolRecService.removeSplRePtlRecVOByIds(new ArrayList<Long>(), null);
            BaseLogUtils.newWebLogger("删除数据").setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setKeys("result", JSONObject.toJSONString(result))
                    .info();
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.newWebLogger("删除数据失败,业务异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.newWebLogger("删除数据失败,系统异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .error(LogExceptionEnum.COMMON_CODE_0099);
        }

        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {

        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("主键不能为空!");
            BaseLogUtils.newWebLogger("获取数据,主键不存在").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0001);
            return result;
        }

        try {
            SplRenewProtocolRecVO vo = this.splRenewProtocolRecService.selectSplRePtlRecVOById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("数据不存在!");
                BaseLogUtils.newWebLogger("根据主键获取对象失败,数据不存在").setLogOperType(LogOperEnum.RESP)
                        .setSysCd(CurrentThreadContext.getCurrentSysCd())
                        .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                        .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                        .setWarning(false)
                        .warn(LogExceptionEnum.COMMON_CODE_0002);
            }
            result.setData(vo);
            BaseLogUtils.newWebLogger("获取数据").setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setKeys("result", JSONObject.toJSONString(result))
                    .info();
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.newWebLogger("根据主键获取对象失败,业务异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.newWebLogger("根据主键获取对象失败,系统异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .error(LogExceptionEnum.COMMON_CODE_0099);
        }

        return result;
    }
    
    /**
     * 到供应商协议记录页面
     */
    @RequestMapping(value = "/toSplProtocolRecPage.do", method = {RequestMethod.GET})
    public ModelAndView toSplProtocolRecPage(HttpServletRequest request, HttpServletResponse response) {

        ModelAndView view = new ModelAndView("spl/splRenewProtocolRec/splProtocolRecPage");
        
        String splId = request.getParameter("splId");
        
        view.addObject("splId", splId);

        return view;
    }
    
    /**
     * 发起续签协议流程
     */
    @RequestMapping(value = "/addRenewProtocolFlow.do", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object addRenewProtocolFlow(HttpServletRequest request, HttpServletResponse response) throws BaseException {

        String id = request.getParameter("id");// 供应商ID
        
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "提交成功!");
        
        if (StringUtils.isEmpty(id)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("请确保您已添加一天新的记录");
            BaseLogUtils.newWebLogger("主键ID为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0001);
			return result;
		}
        
        // 查询当前记录是否在审核中
        SysFlowSHQueryCondition flowCondition = new SysFlowSHQueryCondition();
        flowCondition.setDataId(Long.valueOf(id));
        flowCondition.setSysCd(SplSystemConstant.FL_SYS_CD);
        flowCondition.setWordsInTheTable(SplInfoConstant.SPL_FLOW_S_H_SPL);
        flowCondition.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN);
        List<SysFlowSHDTO> flowSHList = this.flowQueryApiService.searchFlowSHList(flowCondition);
        if (CollectionUtils.isNotEmpty(flowSHList)) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			String info = "已发起协议续签流程,不可重复发起";
			result.setInfo(info);
            BaseLogUtils.newWebLogger("已发起协议续签流程,不可重复发起").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0001);
			return result;
		}
        
        // 查询当前要发起流程的记录
        SplRenewProtocolRecVO vo = this.splRenewProtocolRecService.selectSplRePtlRecVOById(Long.valueOf(id));
        BaseLogUtils.newWebLogger("当前要发起流程的记录查询结果=>" + JSONObject.toJSONString(vo)).setLogOperType(LogOperEnum.RESP)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        
        if (null == vo) {
        	result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("为查询到协议记录");
            BaseLogUtils.newWebLogger("未查询到要发起流程的记录").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return result;
		}
        
        try {
			vo.setAudStsCd("1");// 协议审核状态
			// 发起流程
			result = this.splRenewProtocolRecService.addRenewProtocolFlow(vo, CurrentThreadContext.getCurrentBuOrgCd(), 
					CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
            BaseLogUtils.newWebLogger("发起流程结果=>" + JSONObject.toJSONString(result)).setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .info();
		} catch (BaseException e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("发起流程失败");
            BaseLogUtils.newWebLogger("发起流程异常BaseException,业务异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(e)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return result;
		} catch (Exception e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("发起流程异常");
            BaseLogUtils.newWebLogger("发起流程异常Exception,系统异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(e)
                    .error(LogExceptionEnum.COMMON_CODE_0099);
			return result;
		}
        return result;
    }
    
    /**
     * 到流程表单页面
     */
    @RequestMapping(value = "/toFLowFormPage.do", method = {RequestMethod.GET})
    public ModelAndView toFLowFormPage(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String isModify = request.getParameter("isModify");
        SplRenewProtocolRecVO vo = this.splRenewProtocolRecService.selectSplRePtlRecVOById(Long.valueOf(id));
        ModelAndView view = new ModelAndView("spl/splRenewProtocolRec/splRenewProtocolFlowFormPage");
        view.addObject("splId", vo.getSplId());
        view.addObject("isModify", isModify);

        BaseLogUtils.newWebLogger(" 加载例子详细页面").setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();

        return view;
    }
}

